import 'package:duqing/components/article_card.dart';
import 'package:duqing/components/song_card.dart';
import 'package:duqing/components/video_card.dart';
import 'package:duqing/models/article_model.dart';
import 'package:duqing/models/recommend_interface.dart';
import 'package:duqing/models/song_model.dart';
import 'package:duqing/models/video_model.dart';
import 'package:duqing/services/recommend_service.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class RecommendPage extends StatefulWidget {
  const RecommendPage({Key key}) : super(key: key);

  @override
  _RecommendPageState createState() => _RecommendPageState();
}

class _RecommendPageState extends State<RecommendPage>
    with AutomaticKeepAliveClientMixin {
  EasyRefreshController _controller;
  int page = 1;
  int limit = 10;
  bool hasMore;
  List<RecommendInterface> _list = [];

  Future _onLoad() {
    _getCommendSource(push: true);
    _controller.finishLoad(noMore: !hasMore);
  }

  Future _onRefresh() {
    page = 1;
    _getCommendSource();
    _controller.resetLoadState();
  }

  Future _getCommendSource({bool push = false}) async {
    try {
      List<dynamic> result = await RecommendService.getRecommend(page: page);
      print(result);
      List<RecommendInterface> recommendList = result.map((item) {
        if (item['songEntity'] != null) {
          return SongItem.fromJson(item['songEntity']);
        } else if (item['articleEntity'] != null) {
          return ArticleItem.fromJson(item['articleEntity']);
        } else if (item['videoEntity'] != null) {
          return VideoItem.fromJson(item['videoEntity']);
        }
      }).toList();
      setState(() {
        page++;
        if (push) {
          _list.addAll(recommendList);
        } else {
          _list = recommendList;
        }
      });
    } on Exception catch (e) {
      print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return EasyRefresh(
      firstRefresh: true,
      onRefresh: _onRefresh,
      onLoad: _onLoad,
      controller: _controller,
      child: ListView.builder(
          itemCount: _list.length,
          itemBuilder: (BuildContext context, int index) {
            RecommendInterface interface = _list[index];
            return getItemWidget(interface);
          }),
    );
  }

  Widget getItemWidget(RecommendInterface item) {
    if (item is ArticleItem) {
      return _cloumnBox(ArticleCard(
        articleItem: item,
      ));
    } else if (item is SongItem) {
      return _cloumnBox(SongCard(
        songItem: item,
      ));
    } else if (item is VideoItem) {
      return _cloumnBox(VideoCard(
        videoItem: item,
      ));
    } else {
      return SizedBox.shrink();
    }
  }

  Widget _cloumnBox(Widget card){
    return Column(

      children: [
        SizedBox(height: 8,),
        card,
      ],
    );
  }

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController();
  }

  @override
  void dispose() {
    _controller.dispose();
  }

  @override
  bool get wantKeepAlive => true;
}
